<template>
    <div>
        <div class="searchHeader">
            <el-input placeholder="根据栏目名称搜索" size="small" v-model="cateKeyWords" class="cateKeyWords"></el-input>
            <el-button type="primary" icon="el-icon-search" size="small" @click="initCategory">搜索栏目</el-button>
            <el-button type="primary" icon="el-icon-plus" size="small" @click="showDialog">新增栏目</el-button>
        </div>
        <div>
            <el-table
                    @selection-change="multiSelect"
                    v-loading="loading"
                    :data="tableData"
                    class="formTable">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    prop="id"
                    label="编号"
                    width="80">
            </el-table-column>
            <el-table-column
                    prop="catename"
                    label="栏目名称"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="createtime"
                    label="创建时间"
                    width="140">
            </el-table-column>
            <el-table-column
                    label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="showEditDialog(scope.row)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-button type="danger" size="mini" :disabled="mulBtn.length==0" @click="multiDelete">批量删除</el-button>

        <el-dialog
                title="新增栏目"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <label>栏目名称</label>
                <el-input v-model="addCategory.catename" size="small" placeholder="新增栏目名称" class="addCategoryInput"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
                <el-button type="primary" @click="addCategorySubmit" size="mini">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="修改栏目"
                :visible.sync="dialogVisible2"
                width="30%">
            <div>
                <label>栏目名称</label>
                <el-input v-model="updateCategory.catename" size="small" placeholder="修改栏目名称" class="addCategoryInput"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible2 = false" size="mini">取 消</el-button>
                <el-button type="primary" @click="updateCategoryMethod" size="mini">确 定</el-button>
            </span>
        </el-dialog>
        </div>
    </div>
</template>

<script>
    export default {
        name: "CategoryManage",
        data(){
            return{
                cateKeyWords:'',
                tableData: [],
                loading:true,
                dialogVisible:false,
                addCategory:{
                    catename:''
                },
                mulBtn:[],
                dialogVisible2:false,
                updateCategory:{
                    id:'',
                    catename:''
                }
            }
        },
        methods:{
            initCategory(){
                this.$getRequest('/category/getCategory?keyWords='+this.cateKeyWords).then(resp=>{
                    this.tableData=resp;
                    this.loading=false;
                })
            },
            showDialog(){
                this.dialogVisible=true;
            },
            addCategorySubmit(){
                if(this.addCategory.catename==null || this.addCategory.catename==''){
                    this.$message.warning("栏目名称不能为空！");
                }else{
                    this.$postRequest('/category/addCategory',this.addCategory).then(resp=>{
                        if(resp){
                            this.initCategory();
                            this.dialogVisible=false;
                            this.addCategory.catename='';
                        }

                    })
                }
            },
            handleDelete(row){
                this.$confirm('此操作将永久删除该栏目, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    /*发送请求，判断该栏目id下是否有文章*/
                    this.$getRequest("/article/getArticle/"+row.id).then(resp=>{
                        if(resp.status==500){
                            return false;
                        }else if(resp.status==200){
                            this.$deleteRequest('/category/delCategory/'+row.id).then(resp=>{
                                this.initCategory();
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                })
            },
            multiSelect(selection){
                this.mulBtn = selection
            },
            multiDelete(){
                this.$confirm('此操作将永久删除您选中的栏目，一共'+this.mulBtn.length+'项, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let str = '';
                    this.mulBtn.forEach((item,i)=>{
                        str+='ids='+item.id+'&';
                    })
                    this.$deleteRequest('/category/delCategories?'+str).then(resp=>{
                        this.initCategory();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                })
            },
            showEditDialog(row){
                this.updateCategory.catename=row.catename;
                this.updateCategory.id=row.id;
                this.dialogVisible2=true;
            },
            updateCategoryMethod(){
                this.$putRequest('/category/updateCategory',this.updateCategory).then(resp=>{
                    this.initCategory();
                    this.dialogVisible2=false;
                })
            }
        },
        mounted(){
            this.initCategory();
        }
    }
</script>

<style scoped>
    .searchHeader{
        margin: 15px 0;
    }

    .cateKeyWords{
        width: 300px;
        margin-right: 10px;
    }

    .formTable{
        width: 550px;
        margin-bottom: 15px;
    }

    .addCategoryInput{
        width: 370px;
        margin-top: 10px;
    }
</style>